package com.puzzlebazar.client.puzzle.model;

import java.util.HashMap;
import java.util.Iterator;

import com.puzzlebazar.client.heyawake.HeyawakeFactory;

public class GenreFactory {

	/**
	 * Private constructor for singleton, should register all the 
	 * types of puzzles
	 */
	private GenreFactory() {
		HeyawakeFactory.register(this);
	}

	private static GenreFactory instance = null;
	public static synchronized GenreFactory getInstance() {
		if( instance == null ) instance = new GenreFactory();
		return instance;
	}
	
	private final HashMap< String, PuzzleFactory > factories =
		new HashMap< String, PuzzleFactory >();
	
	/**
	 * Adds a new factory to the list of available factories
	 * 
	 * @param genre The name of the genre for which a factory is provided
	 * @param factory The provided factory
	 */
	public void registerFactory( String genre, PuzzleFactory factory )	{
		factories.put( genre, factory );
	}
	
	/**
	 * Access the factory for a given genre
	 * 
	 * @param genre The name of the genre for which to retrieve a factory
	 * @return The associated factory, if it exists, null if it doesn't
	 */
	public PuzzleFactory getFactory( String genre ) {
		PuzzleFactory factory = factories.get( genre );
		return factory;
	}
	
	/**
	 * @return An array containing all the supported genres 
	 */
	public String[] getGenres() {
		
		String[] genres = new String[ factories.size() ];
		int i = 0;
		for (Iterator<String> iterator = factories.keySet().iterator(); iterator.hasNext();) {
			genres[i] = iterator.next();
			i++;
		}
		
		return genres;
	}
	
}
